package com.yxh.lib_base.ext.xpopup

import android.app.Activity
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.impl.LoadingPopupView

/**
 * @author : hgj
 * @date : 2020/6/28
 */

//loading框
private var loadingDialog: LoadingPopupView? = null

/**
 * 打开等待框
 */
fun AppCompatActivity.showLoadingExt(message: String = "请求网络中") {
    if (!this.isFinishing) {
        if (loadingDialog?.isShow == true) {
            loadingDialog!!.setTitle(message)
        } else {
            loadingDialog?.show()
        }
    }
}

/**
 * 打开等待框
 */
fun Fragment.showLoadingExt(message: String = "请求网络中") {
    activity?.let {
        if (!it.isFinishing) {
            if (loadingDialog == null) {
                loadingDialog = XPopup.Builder(it).dismissOnTouchOutside(false).asLoading(message)
            }
            if (loadingDialog?.isShow == true) {
                loadingDialog!!.setTitle(message)
            } else {
                loadingDialog?.show()
            }
        }
    }
}

/**
 * 关闭等待框
 */
fun Activity.dismissLoadingExt() {
    loadingDialog?.dismiss()
    loadingDialog = null
}

/**
 * 关闭等待框
 */
fun Fragment.dismissLoadingExt() {
    loadingDialog?.dismiss()
    loadingDialog = null
}

fun Fragment.smartDismissLoadingExt() {
    loadingDialog?.smartDismiss()
    loadingDialog = null
}

